<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="net.groupme.model.Resource"%>
<%@page import="net.groupme.model.ObjectFactory"%>
<%@page import="net.groupme.user.UserManager"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="java.util.Collection"%>
<%@page import="net.groupme.model.Tag"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<div id="mainGroupwindowContent" style="margin-top:1em;">
<h4>Add resource to group(s)</h4>

<% 
if(request.getParameter(VIEW_VOCABULARY.PARAMETER_URL) != null && request.getParameter(VIEW_VOCABULARY.PARAMETER_GROUP) == null){ %>
	<p class="error" style="font-size:0.8em; margin-top:1em;"><i>You have to select one group at least!</i></p>
<% } %>
<% Resource resource = ObjectFactory.getResource("" + request.getAttribute(VIEW_VOCABULARY.PARAMETER_ID)); 
 if(resource == null){
	 System.out.println("hier: " );
	 resource = ObjectFactory.getResource(request.getParameter(VIEW_VOCABULARY.PARAMETER_ID)); 
 }
 /* debug:
	 System.out.println("myhome: " + request.getParameter("myhome"));
	 System.out.println("myhome (attr): " + request.getAttribute("myhome"));
	 System.out.println("action: " + request.getParameter("action"));
	 System.out.println("action (attr): " + request.getAttribute("action"));
	 System.out.println("id: " + request.getParameter("id"));
	 System.out.println("id (attr): " + request.getAttribute("id"));
*/
 %>
<form action="<%= VIEW_VOCABULARY.GROUPME_BASE_URL +  VIEW_VOCABULARY.ACTION_ADD_TO_GROUP %>" method="post"> 
	<input type="hidden" name="<%= VIEW_VOCABULARY.PARAMETER_ID %>" value="<%= resource.getId() %>"/>
	<input type="hidden" name="<%= VIEW_VOCABULARY.PARAMETER_TITLE %>" value="<%= resource.getName() %>"/>
	<input type="hidden" name="<%= VIEW_VOCABULARY.PARAMETER_URL %>" value="<%= resource.getURL() %>"/>
	<input type="hidden" name="<%= VIEW_VOCABULARY.PARAMETER_DESCRIPTION %>" value="<%= resource.getDescription() %>"/>
	
			<table style="font-size:0.8em; margin-top:1em;">
				<tr>
					<td><strong>Title:</strong></td> <td><%= resource.getName() %></td>
				</tr>
				<tr>
					<td><strong>URL: </strong></td><td> <a href="<%= resource.getURL() %>"><%= resource.getURL() %></a></td>
				</tr>
				<tr>
					<td><strong>Description: </strong> <br/></td><td> <%= resource.getDescription() %></td>
				</tr>
				<tr>
					<% 
						String keywords = request.getParameter(VIEW_VOCABULARY.PARAMETER_TAGS);
						Collection<Tag> tags = resource.getTags(userManager.getUser()); 
						if(keywords == null){
							keywords = "";
							String tmpKeyword = "";
							int numOfKeywords = 0;
							for(Tag tag: tags){
									tmpKeyword = tag.getKeyword();
									if(tmpKeyword.contains(" ")){
										keywords += "\"" + tmpKeyword + "\"";
									}else{
										keywords += tmpKeyword ;
									}
									numOfKeywords++;
									if(numOfKeywords < tags.size()){
										keywords +=", ";
									}
							}
						}%>
					<td><strong>Tags: </strong></td><td> <input size="60" type="text" name="<%= VIEW_VOCABULARY.PARAMETER_TAGS %>" value="<%= keywords %>"/></td>
				</tr>
				<tr>
					<td></td><td style="color: gray"> <i>Separate tags with spaces. Enclose multi-word tags in "quotes", e.g.: "semantic web" w3c. </i></td>
				</tr>
				<tr><td><strong>Select group(s):</strong></td>
					<td>
						<select name="<%= VIEW_VOCABULARY.PARAMETER_GROUP %>" multiple="multiple" size="5" >							
							<c:forEach items="${userManager.user.groups}" var="group" varStatus="status">
								<option value ="<c:out value='${group.id}' />"><c:out value='${group.name}' /></option>
							</c:forEach>
						</select>
					</td>
				</tr>
				<tr>
					<td></td><td> 
					<input type="button" value="back" onclick="javascript:location.href='<%= request.getContextPath() + "/" + VIEW_VOCABULARY.ACTION_HOME + "?" + VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW + "=" + VIEW_VOCABULARY.VIEW_MYRESOURCE_QUEUE %>'" />
					<input type="submit" value="add" /> </td>
				</tr>
			</table>
			</form>
		
</div>